//
//  Global.cpp
//  kasama1
//
//  Created by 丁逸鹏 on 15/3/3.
//
//

#include "Global.h"
DECLARE_SINGLETON_MEMBER(Global);
Global::Global(void)
{
    srand(time(NULL));
    sprintf(musicpath,"audio/bgm3.mp3");
    volume=UserDefault::getInstance()->getIntegerForKey("volume",50);
    isPlayMusic=UserDefault::getInstance()-> getBoolForKey("isPlay",true);
    historyDialog=cocostudio::GUIReader::getInstance()->widgetFromJsonFile("history/history.ExportJson");
    optionDialog=cocostudio::GUIReader::getInstance()->widgetFromJsonFile("option/option.ExportJson");
    optionDialog->retain();
    historyDialog->retain();
    score1=LabelAtlas::create("0", "shu2.png",26,31,'0');
    score1->setAnchorPoint(Vec2(0.5,0.5));
    historyDialog->addChild(score1);
    //sprintf(scorestr,"%d",element[1]);
    score2=LabelAtlas::create("0", "shu2.png",26,31,'0');
    score2->setAnchorPoint(Vec2(0.5,0.5));
    historyDialog->addChild(score2);
    
    score3=LabelAtlas::create("0", "shu2.png",26,31,'0');
    score3->setAnchorPoint(Vec2(0.5,0.5));
    historyDialog->addChild(score3);
    ui::ImageView * img1=(ImageView* )Helper::seekWidgetByName(historyDialog,"first");
    ui::ImageView * img2=(ImageView* )Helper::seekWidgetByName(historyDialog,"second");
    ui::ImageView * img3=(ImageView* )Helper::seekWidgetByName(historyDialog,"third");
    score1->setPosition(img1->getPosition());
    score2->setPosition(img2->getPosition());
    score3->setPosition(img3->getPosition());
    init();
    
}


Global::~Global(void)
{
    
    delete[] randpatries;
}
void Global::createRandNum(int * a,int n,int min,int max)
{
    int tmp=rand()%(max-min)+min;
    for(int i=0;i<n;i++)
    {
        
        
        while (true)
        {
            bool isRepeat=false;
            a[i]=rand()%(max-min)+min;
            for(int j=0;j<i;j++)
            {
                if(a[i]==a[j])
                {
                    isRepeat=true;
                }
            }
            if(!isRepeat)
            {
                break;
            }
        }
    }
}
void Global::update()
{
    randnum1=rand()%8;
    hardnum+=0.2;
    mapid=rand()%7+1;
    //partriesnum=(int)(hardnum*6)>11?11:(int)(hardnum*6);
    switch (mapid)
    {
        case 1:
            partriesnum=7;
            break;
            
        case 2:
            partriesnum=5;
            break;
        case 3:
            partriesnum=6;
            break;
        case 4:
            partriesnum=6;
            break;
        case 5:
            partriesnum=6;
            break;
        case 6:
            partriesnum=6;
        case 7:
            partriesnum=5;
        default:
            partriesnum=5;
            break;
    }
    randpatries=new int[partriesnum];
    createRandNum(randpatries,partriesnum,0,10);
    
    goalindex=0;
    switch (goalindex)
    {
        case 0:
            needpatriesnum=rand()%4+1;
            needpatriesindexnum=new int[needpatriesnum];
            needpatriesindex=new int[needpatriesnum];
            for(int i=0;i<needpatriesnum;i++)
            {
                needpatriesindex[i]=randpatries[i];
                needpatriesindexnum[i]=15*hardnum-needpatriesnum*1.5;
            }
            
            remainoperate=(20+(needpatriesnum-1)*5)*(hardnum-0.2);
            break;
        default:
            break;
    }
}
void Global::init()
{
    hardnum=0.8;
    score=0;
    update();
}
bool Global::writeScore()
{
    int element[4];
    element[0]=UserDefault::getInstance()->getIntegerForKey("firstscore",0);
    element[1]=UserDefault::getInstance()->getIntegerForKey("secondscore",0);
    element[2]=UserDefault::getInstance()->getIntegerForKey("thirdscore",0);
    element[3]=score;
    Sort(element,4);
    UserDefault::getInstance()->setIntegerForKey("firstscore",element[0]);
    UserDefault::getInstance()->setIntegerForKey("secondscore",element[1]);
    UserDefault::getInstance()->setIntegerForKey("thirdscore",element[2]);
    return true;
}
void Global::Swap(int *a,int *b)
{
    int tmp=*a;
    *a=*b;
    *b=tmp;
    //	cout<<*a<<*b<<endl;
}
void Global::Sort(int A[],int n)
{
    int max;
    for(int i=0;i<n-1;i++)
    {
        max=i;
        for(int j=i+1;j<n;j++)
        {
            if(A[j]>A[max]) 
                max=j;
            
        }
        Swap(&A[i],&A[max]);
    }
}
void Global::initMusic()
{
    if(isPlayMusic){
        CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic(musicpath,true);
        
        CocosDenshion::SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(volume/100.0*3);
    }
}
